import { reactive } from 'vue'
import { getTableDetailReq } from '@/service/modules/data-manage'
import { TableInfo } from '@/service/modules/data-manage/types'
import {
  DataXReaderColumn,
  DataXTaskLog,
  DataXWriterColumn
} from '@/service/modules/task-manage/types'
import { Property } from '@/service/modules/task-instances/types'
import { SelectOption } from 'naive-ui'

interface FieldMap {
  sourceColumnName: string
  sourceDataType: string
  sourceDataFormat: string
  targetColumnName: string
  targetDataType: string
  targetDefVal: string
}

export function useTable() {
  const state = reactive({
    taskLog: {
      versionDesc: '',
      taskDefinedId: 0,
      taskType: 'DATAX',
      taskParams: {
        localParams: Array<Property>(),
        resourceIds: Array<Number>(),
        failRetryTimes: 0,
        failRetryInterval: 0
      },
      taskContent: {
        reader: {
          dbType: null,
          datasourceId: null,
          dbName: null,
          tbName: null,
          where: '',
          splitPk: '',
          resourceId: null,
          resourceName: null,
          resourceUrl: null,
          encoding: 'UTF-8',
          fieldDelimiter: '\\001',
          columns: Array<DataXReaderColumn>()
        },
        writer: {
          dbType: null,
          datasourceId: null,
          dbName: null,
          tbName: null,
          writeMode: 'insert',
          columns: Array<DataXWriterColumn>()
        },
        speedChannel: 1,
        errorLimitRecord: 0,
        errorLimitPercentage: 0.0
      }
    } as unknown as DataXTaskLog,
    sourceTableInfo: {} as TableInfo,
    targetTableInfo: {} as TableInfo,
    fieldMaps: [] as Array<FieldMap>,
    sourceColumnOptions: [] as Array<SelectOption>
  })

  const getSourceTableDetail = async () => {
    const res = await getTableDetailReq({
      datasourceId: state.taskLog.taskContent.reader.datasourceId,
      dbName: state.taskLog.taskContent.reader.dbName,
      tableName: state.taskLog.taskContent.reader.tbName
    })
    if (res.resp_code === 0) {
      state.sourceTableInfo = res.data as TableInfo
      state.sourceColumnOptions.splice(0)
      state.sourceTableInfo.columns.forEach((col) => {
        state.sourceColumnOptions.push({
          label: col.columnName,
          value: col.columnName
        })
      })
    }
  }
  const getTargetTableDetail = async () => {
    const res = await getTableDetailReq({
      datasourceId: state.taskLog.taskContent.writer.datasourceId,
      dbName: state.taskLog.taskContent.writer.dbName,
      tableName: state.taskLog.taskContent.writer.tbName
    })
    if (res.resp_code === 0) {
      state.targetTableInfo = res.data as TableInfo
      //重置字段映射表
      state.fieldMaps.splice(0)
      for (let i = 1; i <= state.targetTableInfo.columns.length; i++) {
        const col = state.targetTableInfo.columns[i - 1]
        state.fieldMaps.push({
          sourceColumnName: `col${i}`,
          sourceDataType: '',
          sourceDataFormat: '',
          targetColumnName: col.columnName,
          targetDataType: col.dataType,
          targetDefVal: ''
        })
      }
    }
  }
  const onOperationClick = (
    index: number,
    type: 'removeField' | 'moveUpField' | 'moveDownField'
  ) => {
    switch (type) {
      case 'removeField':
        state.fieldMaps.splice(index, 1)
        break
      case 'moveUpField':
        if (index === 0) return
        const up1 = state.fieldMaps[index - 1]
        const down1 = state.fieldMaps[index]
        state.fieldMaps[index - 1] = down1
        state.fieldMaps[index] = up1
        break
      case 'moveDownField':
        if (index === state.fieldMaps.length - 1) return
        const up2 = state.fieldMaps[index]
        const down2 = state.fieldMaps[index + 1]
        state.fieldMaps[index] = down2
        state.fieldMaps[index + 1] = up2
        break
    }
  }
  return {
    state,
    getSourceTableDetail,
    getTargetTableDetail,
    onOperationClick
  }
}
